<template>
  <div v-if="visible" class="message-detail-modal" @click.self="closeModal">
    <div class="message-detail-content">
      <div class="message-detail-header">
        <h3><i class="el-icon-message"></i> 消息通知</h3>
        <el-button 
          icon="el-icon-close" 
          circle 
          size="small"
          class="header-close-btn"
          @click="closeModal"
        ></el-button>
      </div>
      <div class="message-detail-body">
        <div class="message-list">
          <div 
            v-for="(message, index) in messages" 
            :key="index" 
            class="message-item"
            @click="handleMessageClick(message)"
          >
            <div class="message-badge" :class="getBadgeClass(message.type)">{{ message.type.charAt(0) }}</div>
            <div class="message-content">
              <h4>{{ message.title }}</h4>
              <p>{{ message.content }}</p>
              <div class="message-footer">
                <span>{{ message.time }}</span>
                <el-link type="primary" :underline="false" v-if="message.link">查看详情 →</el-link>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'MessageDetail',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    messages: {
      type: Array,
      default: () => []
    }
  },
  methods: {
    closeModal() {
      this.$emit('close')
    },
    getBadgeClass(type) {
      const typeMap = {
        '采购通知': 'purple',
        '维修通知': 'blue',
        '系统通知': 'orange',
        '会议通知': 'green'
      }
      return typeMap[type] || 'gray'
    },
    handleMessageClick(message) {
      if (message.link) {
        this.$emit('message-click', message)
        this.closeModal()
      }
    }
  }
}
</script>

<style scoped>
.message-detail-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 2000;
}

.message-detail-content {
  background-color: #fff;
  border-radius: 4px;
  width: 80%;
  max-width: 700px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.message-detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
  border-bottom: 1px solid #e6e6e6;
  background-color: #f8f8f8;
  border-radius: 4px 4px 0 0;
}

.message-detail-header h3 {
  margin: 0;
  font-size: 16px;
  color: #333;
  display: flex;
  align-items: center;
  font-weight: normal;
}

.message-detail-header h3 i {
  margin-right: 8px;
  color: #666;
}



.message-detail-body {
  padding: 20px;
  max-height: 60vh;
  overflow-y: auto;
}

.message-list {
  padding: 8px 0;
}

.message-item {
  display: flex;
  padding: 12px 16px;
  border-bottom: 1px solid #f0f0f0;
  transition: all 0.3s;
  cursor: pointer;
}

.message-item:last-child {
  border-bottom: none;
}

.message-item:hover {
  background-color: #f5f7fa;
}

.message-badge {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  margin-right: 12px;
  flex-shrink: 0;
  font-size: 12px;
}

.message-badge.purple {
  background-color: #9c27b0;
}

.message-badge.blue {
  background-color: #2196f3;
}

.message-badge.orange {
  background-color: #ff9800;
}

.message-badge.green {
  background-color: #4caf50;
}

.message-badge.gray {
  background-color: #9e9e9e;
}

.message-content {
  flex: 1;
}

.message-content h4 {
  margin: 0 0 4px 0;
  font-size: 14px;
  color: #333;
}

.message-content p {
  margin: 0 0 6px 0;
  font-size: 13px;
  color: #666;
  line-height: 1.4;
}

.message-footer {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: #999;
}
</style>